﻿using System;
using System.IO;
using System.Windows.Forms;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.Data;
using System.Diagnostics;
using System.Drawing;

namespace MakeIT_Bill_Maker.Report
{
    class PdfStatistics
    {
        private Database.DatabaseConnection db = new Database.DatabaseConnection();

        public void printPdf(DataTable dt, DataTable dt1, string m, string y)
        {
            try
            {
                if (m.Equals("0"))
                {
                    y = (int.Parse(y) - 1).ToString();
                    m = "12";
                }

                int totalArrears=0, totalConsumption=0, totalCharges=0, totalDue=0;
                Document oDoc = new Document(PageSize.A4, 30, 30, 30, 30);
                PdfWriter.GetInstance(oDoc, new FileStream("STATISTICS_OF_" + y + "_" + m + ".pdf", FileMode.Create));
                oDoc.Open();

                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    totalArrears += int.Parse(dt.Rows[i]["m.Arrears"].ToString());
                    totalConsumption += int.Parse(dt.Rows[i]["UnitsConsumed"].ToString());
                    totalCharges += int.Parse(dt.Rows[i]["Total"].ToString());
                    totalDue += int.Parse(dt.Rows[i]["m.Arrears"].ToString()) + int.Parse(dt.Rows[i]["Total"].ToString());
                }

                // Taking month in text
                string mt = "";
                switch (m)
                {
                    case "1": mt = "JANUARY"; break;
                    case "2": mt = "FEBRUARY"; break;
                    case "3": mt = "MARCH"; break;
                    case "4": mt = "APRIL"; break;
                    case "5": mt = "MAY"; break;
                    case "6": mt = "JUNE"; break;
                    case "7": mt = "JULY"; break;
                    case "8": mt = "AUGUST"; break;
                    case "9": mt = "SEPTEMBER"; break;
                    case "10": mt = "OCTOBER"; break;
                    case "11": mt = "NOVEMBER"; break;
                    case "12": mt = "DECEMBER"; break;
                }

                Phrase newline = new Phrase("\n");

                // Start PDF header
                Paragraph head1 = new Paragraph("EDMONTON APARTMENTS", new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.COURIER, 16));
                Paragraph head2 = new Paragraph("WATER USE STATISTICS - " + mt + " " + y + "\n", new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.COURIER, 16, iTextSharp.text.Font.BOLD));
                head1.Alignment = (Element.ALIGN_CENTER);
                head2.Alignment = (Element.ALIGN_CENTER);
                oDoc.Add(head1);
                oDoc.Add(head2);
                // End PDF header

                oDoc.Add(newline);
                oDoc.Add(newline);

                PdfPTable table2 = new PdfPTable(2);
                table2.TotalWidth = (900);

                // TOTAL CONSUMPTION 
                table2.DefaultCell.FixedHeight = 25;
                table2.DefaultCell.HorizontalAlignment = (Element.ALIGN_LEFT);
                table2.DefaultCell.VerticalAlignment = (Element.ALIGN_MIDDLE);
                table2.DefaultCell.BackgroundColor = new iTextSharp.text.BaseColor(Color.LightGray);
                table2.AddCell("Total Consumption");
                table2.DefaultCell.HorizontalAlignment = (Element.ALIGN_RIGHT);
                table2.DefaultCell.VerticalAlignment = (Element.ALIGN_MIDDLE);
                table2.DefaultCell.BackgroundColor = new iTextSharp.text.BaseColor(Color.White);
                table2.AddCell(totalConsumption.ToString());

                // TOTAL CHARGES
                table2.DefaultCell.FixedHeight = 25;
                table2.DefaultCell.HorizontalAlignment = (Element.ALIGN_LEFT);
                table2.DefaultCell.VerticalAlignment = (Element.ALIGN_MIDDLE);
                table2.DefaultCell.BackgroundColor = new iTextSharp.text.BaseColor(Color.LightGray);
                table2.AddCell("Total Charges");
                table2.DefaultCell.HorizontalAlignment = (Element.ALIGN_RIGHT);
                table2.DefaultCell.VerticalAlignment = (Element.ALIGN_MIDDLE);
                table2.DefaultCell.BackgroundColor = new iTextSharp.text.BaseColor(Color.White);
                table2.AddCell(totalCharges.ToString() + ".00");

                // TOTAL ARREARS
                table2.DefaultCell.FixedHeight = 25;
                table2.DefaultCell.VerticalAlignment = (Element.ALIGN_MIDDLE);
                table2.DefaultCell.HorizontalAlignment = (Element.ALIGN_LEFT);
                table2.DefaultCell.BackgroundColor = new iTextSharp.text.BaseColor(Color.LightGray);
                table2.AddCell("Total Arrears");
                table2.DefaultCell.HorizontalAlignment = (Element.ALIGN_RIGHT);
                table2.DefaultCell.VerticalAlignment = (Element.ALIGN_MIDDLE);
                table2.DefaultCell.BackgroundColor = new iTextSharp.text.BaseColor(Color.White);
                table2.AddCell(totalArrears.ToString() + ".00");

                // TOTAL DUE
                table2.DefaultCell.FixedHeight = 25;
                table2.DefaultCell.HorizontalAlignment = (Element.ALIGN_LEFT);
                table2.DefaultCell.VerticalAlignment = (Element.ALIGN_MIDDLE);
                table2.DefaultCell.BackgroundColor = new iTextSharp.text.BaseColor(Color.LightGray);
                table2.AddCell("Total Due");
                table2.DefaultCell.HorizontalAlignment = (Element.ALIGN_RIGHT);
                table2.DefaultCell.VerticalAlignment = (Element.ALIGN_MIDDLE);
                table2.DefaultCell.BackgroundColor = new iTextSharp.text.BaseColor(Color.White);
                table2.AddCell(totalDue.ToString() + ".00");

                // TOTAL PAYMENTS
                table2.DefaultCell.FixedHeight = 25;
                table2.DefaultCell.HorizontalAlignment = (Element.ALIGN_LEFT);
                table2.DefaultCell.VerticalAlignment = (Element.ALIGN_MIDDLE);
                table2.DefaultCell.BackgroundColor = new iTextSharp.text.BaseColor(Color.LightGray);
                table2.AddCell("Total Payments");
                table2.DefaultCell.HorizontalAlignment = (Element.ALIGN_RIGHT);
                table2.DefaultCell.VerticalAlignment = (Element.ALIGN_MIDDLE);
                table2.DefaultCell.BackgroundColor = new iTextSharp.text.BaseColor(Color.White);
                table2.AddCell(dt1.Rows[0]["TotalPayments"].ToString() + ".00");

                oDoc.Add(table2);
                oDoc.Close();

                // Setting the Total Payments to zero
                string sql = "UPDATE OtherFixed SET TotalPayments = 0 WHERE Index=1";
                bool b = db.edit(sql);

                MessageBox.Show("Report is Succesfully Generated.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
                Process.Start(Application.StartupPath + "/STATISTICS_OF_" + y + "_" + m + ".pdf");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        public void printPdf()
        {
            Statistics f = new Statistics();
            f.Show();
        }
    }
}
